<?php
/**
 * Created by PhpStorm.
 * User: linyh
 * Date: 2017/11/25 0025 10:13
 */
?>
<link rel="stylesheet" href="/css/dataTables.bootstrap.min.css">
<!--<div class="content-wrapper">-->
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        贷款审核
    </h1>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-body">
                <table id="table" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>编号</th>
                        <th>申请用户</th>
                        <th>贷款金额</th>
                        <th>贷款周期</th>
                        <th>贷款利率</th>
                        <th>到期利息</th>
                        <th>提交时间</th>
                        <th>审核时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</section>

<script src="/js/jquery.dataTables.min.js"></script>
<script src="/js/dataTables.bootstrap.min.js"></script>
<script>
var $table = $('#table');
var $tr = $table.find('tbody tr').remove();
$.ajax('/api/country/user-loan', {
    type: 'GET',
    data: {
    },
    dataType: 'json',
    headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
}).then(function(ret) {
    if(ret.state ==0) {
        data = ret.data;
        $('#table').DataTable({
            "columnDefs": [
                {
                    "targets": 3,
                    "data": "title",
                    "render": function ( data, type, row, meta ) {
                        return data + '个财年';
                    }
                },
                {
                    "targets": 8,
                    "data": "title",
                    "render": function ( data, type, row, meta ) {
                        var strings = {
                            '1':'待审核',
                            '2':'已取消',
                            '3':'已通过',
                            '4':'已偿还',
                            '5':'已拒绝',
                        }
                        return strings[data];
                    }
                },
                {
                    "targets": 9,
                    "data": "title",
                    "render": function ( data, type, row, meta ) {
                        if(row.status == 1 && global.user.role==3) {
                            return '<button class="btn btn-success btn-sm" lk-id="' + row.id + '" lk-role="auth">同意</button>' +
                                '<button class="btn btn-success btn-sm" lk-id="' + row.id + '" lk-role="reject">拒绝</button>';
                        } else {
                            return '-';
                        }
                    }
                },
            ],
            data: data.list,
            columns: [
                { data: 'id' },
                { data: 'user.username' },
                { data: 'num' },
                { data: 'cycle' },
                { data: 'rate' },
                { data: 'interest' },
                { data: 'apply_time' },
                { data: 'check_time' },
                { data: 'status' },
                { data: 'id' },
            ],
            'paging'      : true,
            'ordering'    : true,
            'info'        : true,
            'autoWidth'   : false
        })
        $("table tr th").css({"text-align":"center"});
        $("table").css({"text-align":"center"});
    } else {
        alert(ret.message);
    }
});
$(document).on('click', '[lk-role="auth"]', function() {
    $.ajax('/api/loan/auth', {
        type: 'post',
        data: {
            'id': $(this).attr('lk-id')
        },
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('申请已同意');
            location.reload();
        } else {
            alert(ret.message);
        }
    });
})
$(document).on('click', '[lk-role="reject"]', function() {
    $.ajax('/api/loan/reject', {
        type: 'post',
        data: {
            'id': $(this).attr('lk-id')
        },
        dataType: 'json',
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}
    }).then(function(ret) {
        if(ret.state ==0) {
            alert('申请已拒绝');
            location.reload();
        } else {
            alert(ret.message);
        }
    });
})
</script>